package com.lagou.edu.homework.service.impl;

import com.lagou.edu.homework.dao.LagouAuthCodeDao;
import com.lagou.edu.pojo.LagouAuthCode;
import com.lagou.edu.service.CodeDubboService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;

import java.util.Optional;

@Service
public class CodeDubboServiceImpl implements CodeDubboService {

    @Autowired
    private LagouAuthCodeDao authCodeDao;

    /**
     * 校验邮箱和验证码
     *
     * @param email
     * @param code
     * @return
     */
    @Override
    public Boolean validateEmailAndCode(String email, String code) {
        LagouAuthCode authCode = new LagouAuthCode();
        authCode.setEmail(email);
        authCode.setCode(code);

        Example<? extends LagouAuthCode> example = Example.of(authCode);
        Optional<? extends LagouAuthCode> authCodeOptional = authCodeDao.findOne(example);
        if (authCodeOptional.isPresent()) {
            LagouAuthCode authCodeOne = authCodeOptional.get();
            return authCodeOne.getExpiretime() > System.currentTimeMillis();
        }
        return false;
    }
}
